/**
 * Created with JetBrains WebStorm.
 * User: renji
 * Date: 15/5/13
 * Time: 7:24 AM
 * To change this template use File | Settings | File Templates.
 */

describe("AuthoriseTransactionDetailsController", function() {

    var authoriseTransactionDetailsController = null;
    var objE = {errorCode:'code', errorMessage:'message'};
    var transfer = null;

    beforeEach(function() {
        var t_alertCtrl = App.AlertController.create();
        var t_loadingCtrl = App.LoadingController.create();
        transfer = App.BillPayment.create();

        App.AuthoriseTransactionDetailsController.reopen({
            init:function(){
                this.set('controllers.alert', t_alertCtrl);
                this.set('controllers.loading', t_loadingCtrl);
                this.set('transfer', transfer);
            }
        });

        spyOn(App.getSession(),'getCurrentTransfer').andReturn(transfer);
        authoriseTransactionDetailsController = App.AuthoriseTransactionDetailsController.create();
    });

    it("prepareController", function(){
        authoriseTransactionDetailsController.prepareController();
    });

    it("acceptTransaction", function() {
        spyOn(authoriseTransactionDetailsController, 'transitionTo');
        authoriseTransactionDetailsController.acceptTransaction();
        expect(authoriseTransactionDetailsController.transitionTo).toHaveBeenCalledWith('authorise.transactionConfirm');
    });

    it("declineTransaction", function() {
        spyOn(authoriseTransactionDetailsController, 'showLoading');
        var transferResult = [];

        spyOn(authoriseTransactionDetailsController, 'declineTransactionCallback');
        spyOn(App.api_authoriseTransaction,'declineTransaction').andCallFake(function(transferResult, callback) {
            callback(transferResult,objE);
        });


        authoriseTransactionDetailsController.declineTransaction();
        expect(App.api_authoriseTransaction.declineTransaction).toHaveBeenCalled();
        expect(authoriseTransactionDetailsController.showLoading).toHaveBeenCalled();
        expect(authoriseTransactionDetailsController.declineTransactionCallback).toHaveBeenCalled();
    });

    it("declineTransactionCallback", function(){

        var error_msg = 'You have declined your transaction. The eTAC code for this transaction has been deactivated.';
        spyOn(authoriseTransactionDetailsController, 'hasError').andReturn(false);
        spyOn(authoriseTransactionDetailsController, 'hideLoading');
        spyOn(authoriseTransactionDetailsController, 'transitionTo');

        spyOn(authoriseTransactionDetailsController.get('controllers').get('alert'), 'showAlert').andCallFake(function(message, callback){
            callback();
        });

        authoriseTransactionDetailsController.declineTransactionCallback([], objE);
        expect(authoriseTransactionDetailsController.hasError).toHaveBeenCalledWith(objE);
        expect(authoriseTransactionDetailsController.hideLoading).toHaveBeenCalled();
        expect(authoriseTransactionDetailsController.get('controllers').get('alert').showAlert).toHaveBeenCalledWith(error_msg, jasmine.any(Function));
        expect(authoriseTransactionDetailsController.transitionTo).toHaveBeenCalledWith('authorise.transactions',{needsRefresh: true});

    });


    it("requestETac", function() {
        spyOn(authoriseTransactionDetailsController, 'showLoading');
        var transferResult = [];

        spyOn(authoriseTransactionDetailsController, 'requestETacCallback');
        spyOn(App.api_authoriseTransaction,'regenerateETAC').andCallFake(function(transferResult, callback) {
            callback(transferResult,objE);
        });


        authoriseTransactionDetailsController.requestETac();
        expect(App.api_authoriseTransaction.regenerateETAC).toHaveBeenCalled();
        expect(authoriseTransactionDetailsController.showLoading).toHaveBeenCalled();
        expect(authoriseTransactionDetailsController.requestETacCallback).toHaveBeenCalled();
    });

    it("requestETacCallback - ERR_ETAC_DECLINED", function(){

        var objE1 =  BreezeException.create({
            errorCode:'ERR_ETAC_REGENERATE_DECLINED',
            errorMessage: 'Error Messge'
        });

        spyOn(authoriseTransactionDetailsController.get('controllers').get('alert'), 'showAlert').andCallFake(function(message, callback){
            callback();
        });

        spyOn(authoriseTransactionDetailsController, 'transitionTo');
        spyOn(authoriseTransactionDetailsController, 'hideLoading');

        authoriseTransactionDetailsController.requestETacCallback([], objE1);

        expect(authoriseTransactionDetailsController.hideLoading).toHaveBeenCalled();
        expect(authoriseTransactionDetailsController.get('controllers').get('alert').showAlert).toHaveBeenCalledWith(objE1.getCustomErrorMessage('GENERAL_EXCEPTION'), jasmine.any(Function));
        expect(authoriseTransactionDetailsController.transitionTo).toHaveBeenCalledWith('authorise.transactions',{needsRefresh: true});

    });

    it("requestETacCallback - hasError - True", function(){

        var objE1 =  BreezeException.create({
            errorCode:'code',
            errorMessage: 'Error Messge'
        });

        spyOn(authoriseTransactionDetailsController, 'hasError').andReturn(true);
        spyOn(authoriseTransactionDetailsController, 'hideLoading');

        authoriseTransactionDetailsController.requestETacCallback([], objE1);

        expect(authoriseTransactionDetailsController.hasError).toHaveBeenCalledWith(objE1);

        expect(authoriseTransactionDetailsController.hideLoading).toHaveBeenCalled();

    });
});